package com.cheyun.netsalev3.view


import android.graphics.drawable.Drawable
import android.util.Log
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.GridLayoutManager
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.databinding.FragmentHomeAnalysisBinding
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.HomeAnalysisFragmentViewModel


class HomeAnalysisFragment : BaseFragment<FragmentHomeAnalysisBinding>() {
    companion object {
        fun newInstance() = HomeAnalysisFragment()
    }

    override fun initContentView(): Int {
        return R.layout.fragment_home_analysis
    }

    override fun initViewModel(): ViewModel? {
        var factory = InjectorUtils.getHomeAnalysisFragmentViewModelFactory()
        return ViewModelProviders.of(this, factory).get(HomeAnalysisFragmentViewModel::class.java)
    }

    private lateinit var adapter: MyAdapter

    override fun initView() {
        binding.recyclerView.layoutManager = GridLayoutManager(context, 2)
        adapter = MyAdapter(R.layout.home_analysis_content_item, binding.viewModel)
        binding.recyclerView.adapter = adapter

//        binding.swipeRefreshLayout.setOnRefreshListener {
//            binding.viewModel?.refresh()
//        }
    }

    override fun initData() {
        binding.viewModel?.showlistData?.observe(this, Observer { list ->
            adapter.submitList(list)
//            binding.swipeRefreshLayout.isRefreshing = false
        })
        val drawables = binding.nameText.compoundDrawables
        binding.viewModel?.infotypename?.observe(this, Observer {
            when (it) {
                "展厅" -> {
                    var drawable = ContextCompat.getDrawable(this!!.context!!, R.drawable.c01_fxsy_exhibitionhall)
                    setIntrinsicBounds(drawable)
                    binding.nameText.setCompoundDrawables(drawable, drawables[1], drawables[2], drawables[3])
                }
                "网电销" -> {
                    var drawable = ContextCompat.getDrawable(this!!.context!!, R.drawable.c01_fxsy_onlinesales)
                    setIntrinsicBounds(drawable)
                    binding.nameText.setCompoundDrawables(drawable, drawables[1], drawables[2], drawables[3])
                }
                "二网" -> {
                    var drawable = ContextCompat.getDrawable(this!!.context!!, R.drawable.c01_fxsy_secondnetwork)
                    setIntrinsicBounds(drawable)
                    binding.nameText.setCompoundDrawables(drawable, drawables[1], drawables[2], drawables[3])
                }

            }
        })
    }

    override fun lazyInit() {
        Log.i(TAG,"override lazyinit")
    }

    private fun setIntrinsicBounds(drawable: Drawable?) {
        drawable?.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
    }


}
